package com.aceitunaproject.gameclient;

import com.aceitunaproject.clientinterface.AceitunaClientInterfaceImpl;
import com.aceitunaproject.clientinterface.eventadapters.AceitunaConnectionEventsHandler;
import com.aceitunaproject.clientinterface.eventregistering.AceitunaAllEventsHandler;
import com.aceitunaproject.shared.gamelement.Settlement;
import com.aceitunaproject.shared.gamelement.Unit;
import com.aceitunaproject.shared.message.concrete.ChatMessageReceivedEvent;
import com.aceitunaproject.shared.message.concrete.MapAreaRefreshEvent;
import com.aceitunaproject.shared.message.concrete.RuleViolationEvent;

/**
 * Handles interaction between the game client and the server
 * 
 * @author Federico Bergstein
 * 
 */
public class AceitunaInterface implements AceitunaAllEventsHandler,
		AceitunaConnectionEventsHandler {

	private GameClient controller;
	private AceitunaClientInterfaceImpl server;

	public AceitunaInterface(String username, String password,
			GameClient gameClient) {
		controller = gameClient;
		server = new AceitunaClientInterfaceImpl(username);
		server.registerForConnectionEvents(this);
		server.registerToEvent(this);
		server.login();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void ChatMessageReceived(ChatMessageReceivedEvent message) {
		// TODO Auto-generated method stub

	}

	@Override
	public void MapAreaRefresh(MapAreaRefreshEvent message) {
		controller.mapRefreshed(message.getAreaTiles());

	}

	@Override
	public void RuleViolationEventReceived(RuleViolationEvent message) {
		controller.ruleViolation(message.getRuleViolations());

	}

	@Override
	public void disconnected(boolean arg0, String arg1) {
		controller.disconnected(arg0, arg1);

	}

	@Override
	public void loggedIn() {
		controller.loggedIn();
		server.requestMapRefresh();

	}

	@Override
	public void loginFailed(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void reconnected() {
		// TODO Auto-generated method stub

	}

	@Override
	public void reconnecting() {
		// TODO Auto-generated method stub

	}

	public void moveUnit(Long unitID, int x, int y) {
		server.moveUnit(unitID, x, y);
	}

	public void requestMapRefresh() {
		server.requestMapRefresh();

	}

	public void buildSettlement(Unit theUnit, String name) {
		server.buildSettlement(theUnit.getId(), name, theUnit.getPositionX(),
				theUnit.getPositionY());

	}

	public void buildInstallation(Unit theUnit, String name,
			String installationType) {
		server.buildInstallation(theUnit.getId(), name, theUnit.getPositionX(),
				theUnit.getPositionY(), installationType);

	}

	public void buildUnit(Settlement theSettlement, String name, int persons) {
		server.recruitUnit(theSettlement.getId(), name, persons);

	}
	
	public void attackUnit(Long attacker,Long defender)
	{
		server.attackUnit(attacker, defender);
	}

}
